package it.isa.demo.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author Corgile
 * @date 2022-02-26
 */
@Component
@Aspect
public class MyLogger {
	@Pointcut(value = "execution(* it.isa.demo.service.*.*(..))")
	public void pointCutForServices() { }

	@Before(value = "pointCutForServices()")
	public void before(JoinPoint joinPoint) {
		String name = joinPoint.getSignature().getName();
		System.out.println("before-----" + name);
	}

	@After(value = "pointCutForServices()")
	public void after(JoinPoint joinPoint) {
		String name = joinPoint.getSignature().getName();
		System.out.println("after-----" + name);
	}

	@AfterReturning(value = "pointCutForServices()", returning = "result")
	public void afterReturning(JoinPoint joinPoint, Object result) {
		String name = joinPoint.getSignature().getName();
		System.out.println("afterReturning----" + name + "---" + result);
	}

	@AfterThrowing(value = "pointCutForServices()", throwing = "e")
	public void afterThrowing(JoinPoint joinPoint, Exception e) {
		String name = joinPoint.getSignature().getName();
		System.out.println("afterThrowing---" + name + "----" + e.getMessage());
	}

	@Around(value = "pointCutForServices()")
	public Object around(ProceedingJoinPoint pjp) throws Throwable {
		return pjp.proceed();
	}
}
